package com.sskj.xgja.transaction;

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.sskj.xgja.MVPFragment;
import com.sskj.base.adapter.ArrowAdapter;
import com.sskj.base.utils.ClickUtil;
import com.sskj.xgja.R;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/20
 */
public class SellOutFragment extends MVPFragment<SellOutPresenter> {

    @BindView(R.id.spinner_type)
    Spinner spinnerType;
    @BindView(R.id.tv_price_type)
    TextView tvPriceType;
    @BindView(R.id.et_price)
    TextView etPrice;
    @BindView(R.id.tv_subtract)
    TextView tvSubtract;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_add)
    TextView tvAdd;

    private BottomSheetDialog bottomSheetDialog;
    private TextView tv_market_price;
    private TextView tv_limited_price;
    private TextView tv_cancel;

    private int amout = 1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_sell_out;
    }

    @Override
    public SellOutPresenter getPresenter() {
        return new SellOutPresenter();
    }

    @Override
    public void initView() {
        List<String> payTypes = Arrays.asList(getResources().getStringArray(R.array.app_transaction_titles));
        ArrowAdapter spinnerTypeAdapter = new ArrowAdapter(getActivity(),
                R.layout.item_spinner_up, payTypes);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.item_spinner_down);
        spinnerType.setAdapter(spinnerTypeAdapter);
        spinnerType.setSelection(0);
        spinnerType.setDropDownVerticalOffset(10);
        RxAdapterView.itemSelections(spinnerType)
                .subscribe(pos -> {
                    switch (pos) {
                        case 0:
//                            mPresenter.getUserInfo("pb");//JC
                            break;
                        case 1:
//                            mPresenter.getUserInfo("cny");//USDT
                            break;
                        default:
                            break;
                    }
                });
        ClickUtil.click(tvPriceType, view -> showBottomDialog());

    }

    @Override
    public void initData() {
        tvAmount.setText(String.valueOf(amout));
        ClickUtil.click(200, tvAdd, view -> {
            amout++;
            tvAmount.setText(String.valueOf(amout));
        });
        ClickUtil.click(200, tvSubtract, view -> {
            if (amout > 1) {
                amout--;
                tvAmount.setText(String.valueOf(amout));
            }
        });

    }

    private void showBottomDialog() {
        bottomSheetDialog = new BottomSheetDialog(getActivity());
        View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_price_type, null);
        tv_market_price = dialogView.findViewById(R.id.tv_market_price);
        tv_limited_price = dialogView.findViewById(R.id.tv_limited_price);
        tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        ClickUtil.click(tv_market_price, view -> {
            tvPriceType.setText("市价");
            etPrice.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_eeeeee));
            etPrice.setEnabled(false);
            bottomSheetDialog.dismiss();
            bottomSheetDialog = null;
        });
        ClickUtil.click(tv_limited_price, view -> {
            tvPriceType.setText("限价");
            etPrice.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.edit_bg));
            etPrice.setEnabled(true);
            bottomSheetDialog.dismiss();
            bottomSheetDialog = null;

        });
        ClickUtil.click(tv_cancel, view -> {
            tvPriceType.setText("限价");
            bottomSheetDialog.dismiss();
            bottomSheetDialog = null;
        });
        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.show();

    }


    public static SellOutFragment newInstance() {
        SellOutFragment fragment = new SellOutFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

}
